
#include <QtGui>

#include "renderarea.h"

//! [0]
RenderArea::RenderArea(QWidget *parent)
    : QWidget(parent)
{
    antialiased = true;
    real(MapSize)=imag(MapSize)=0;
    Map=NULL;
    setBackgroundRole(QPalette::Window);
    setAutoFillBackground(true);
    setPen(QPen(Qt::black, 0));
}

QSize RenderArea::minimumSizeHint() const
{
    return QSize(200, 100);
}

QSize RenderArea::sizeHint() const
{
    return QSize(600, 300);
}

void RenderArea::setPen(const QPen &pen)
{
    this->pen = pen;
    update();
}


void RenderArea::setMap(const std::complex<int> _mapsize,int** _map)
{
    this->MapSize=_mapsize;
    this->Map = new int*[real(_mapsize)];
    //std::cout<<"kopiowano"<<std::endl;
    for (int i=0; i<real(_mapsize); i++) (this->Map)[i]=new int[imag(_mapsize)];
    for(int i=0;i<real(_mapsize);i++)
      for(int j=0;j<imag(_mapsize);j++){
	//std::cout<<"kopiowano "<<i<<" "<<j<<std::endl;
	(this->Map)[i][j]=_map[j][i];}
      //std::cout<<"skopiowano"<<std::endl;
    update();
}

void RenderArea::setRobots(const std::list<robot*> _robots)
{
  this->Robots=_robots;
  update();
  
}
void RenderArea::paintEvent(QPaintEvent * /* event */)
{
    if(real(MapSize)&&imag(MapSize)){

    int xbracketsize=width()/real(MapSize);
    int ybracketsize=height()/imag(MapSize);
    QRect rect(0, 0, xbracketsize, ybracketsize);
    
    

//! [8]

//! [9]
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(Qt::white, Qt::SolidPattern));
    painter.drawRect(QRect(0, 0, xbracketsize*real(MapSize)-1, ybracketsize*imag(MapSize)-1));
    painter.setPen(pen);
    if (antialiased)
        painter.setRenderHint(QPainter::Antialiasing, true);

    float factor = 0.75*rect.width() / (painter.fontMetrics().width(QString::number(22)));
    if ((factor < 1) || (factor > 1.25))
    {
      QFont f = painter.font();
      f.setPointSizeF(f.pointSizeF()*factor);
      painter.setFont(f);
    }
    
    std::complex<double> tRobotPosition;
    std::complex<int> tAproxRobotPosition;
    std::complex<double> tA,tB,tC,tD;
    std::complex<int> tAproxA,tAproxB,tAproxC,tAproxD;

    int tPriority;
      QLinearGradient linearGradient(0, 0, 2*xbracketsize, 2*ybracketsize);
      linearGradient.setColorAt(0.0, Qt::white);
      linearGradient.setColorAt(0.2, Qt::red);
      linearGradient.setColorAt(1.0, Qt::black);
    std::list<robot*>::iterator it = Robots.begin();
    while(it != Robots.end()){
      tRobotPosition=(*it)->getCurrentPlace();
      tA=(*it)->getA();
      tB=(*it)->getB();
      tC=(*it)->getC();
      tD=(*it)->getD();
      tPriority=(*it)->getPriority();
      tAproxA=complex<int>((int)(imag(tA)*((double)xbracketsize)),(int)((real(tA))*((double)ybracketsize)));//Podmiana...
      tAproxB=complex<int>((int)(imag(tB)*((double)xbracketsize)),(int)((real(tB))*((double)ybracketsize)));//Podmiana...
      tAproxC=complex<int>((int)(imag(tC)*((double)xbracketsize)),(int)((real(tC))*((double)ybracketsize)));//Podmiana...
      tAproxD=complex<int>((int)(imag(tD)*((double)xbracketsize)),(int)((real(tD))*((double)ybracketsize)));//Podmiana...
      painter.save();
      painter.translate(real(tAproxA), imag(tAproxA));
      linearGradient.setColorAt(0.2, Qt::green);
      painter.setBrush(linearGradient);
      painter.drawRect(rect);
      painter.drawText(rect, Qt::AlignCenter, QString("A")+QString::number(tPriority));
      painter.restore();    
      painter.save();
      painter.translate(real(tAproxB), imag(tAproxB));
      linearGradient.setColorAt(0.2, Qt::yellow);
      painter.setBrush(linearGradient);
      painter.drawRect(rect);
      painter.drawText(rect, Qt::AlignCenter, QString("B")+QString::number(tPriority));
      painter.restore();    
      painter.save();
      painter.translate(real(tAproxC), imag(tAproxC));
      linearGradient.setColorAt(0.2, Qt::blue);
      painter.setBrush(linearGradient);
      painter.drawRect(rect);
      painter.drawText(rect, Qt::AlignCenter, QString("C")+QString::number(tPriority));
      painter.restore();    
      painter.save();
      painter.translate(real(tAproxD), imag(tAproxD));
      linearGradient.setColorAt(0.2, Qt::cyan);
      painter.setBrush(linearGradient);
      painter.drawRect(rect);
      painter.drawText(rect, Qt::AlignCenter, QString("D")+QString::number(tPriority));
      painter.restore();

      ++it;
    }
    it = Robots.begin();
    while(it != Robots.end()){  
      tRobotPosition=(*it)->getCurrentPlace();
      tPriority=(*it)->getPriority();
      tAproxRobotPosition=complex<int>((int)(imag(tRobotPosition)*((double)xbracketsize)),(int)((real(tRobotPosition))*((double)ybracketsize)));
      painter.save();
      painter.translate(real(tAproxRobotPosition), imag(tAproxRobotPosition));
      linearGradient.setColorAt(0.2, Qt::red);
      painter.setBrush(linearGradient);
      painter.drawEllipse(rect);
      painter.drawText(rect, Qt::AlignCenter, QString("R")+QString::number(tPriority));
      painter.restore();
      ++it;
    }
    
    for (int x = 0; x < real(MapSize); x++) {
      for (int y = 0; y < imag(MapSize); y++) {
	painter.save();
	painter.translate(xbracketsize*x, ybracketsize*y);
	if(Map[x][y]==1)
	  painter.setBrush(QBrush(Qt::black, Qt::SolidPattern));
	else
	  painter.setBrush(QBrush(Qt::NoBrush));
	painter.drawRect(rect);
	
	painter.restore();
      }
    }

    }
}

//! [13]
